package wyu.controller;

import com.dto.Result;
import com.entity.User;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import wyu.service.UserService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Resource
    UserService userService;

    @PostMapping("/login")
    public Result login(@RequestParam("name")String name,
                        @RequestParam("password")String password,
                        HttpServletRequest request){
        User user = userService.login(name, password);
        request.getSession().setAttribute("user",user);
        return Result.ok(user);
    }
    @GetMapping("/getSpUserById/{uname}")
    public Result getUserById(@PathVariable("uname")String uName){
        return Result.ok(userService.getUserByUname(uName));
    }
    @GetMapping("/getUserByUname")
    public User getUserByUname(@RequestParam("uName")String uName){
        return userService.getUserByUname(uName);
    }

    @GetMapping("/getUser")
    public User getUser(HttpServletRequest request){
        return (User) request.getSession().getAttribute("user");
    }

    @GetMapping("/signOut")
    public Result signOut(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        if (request.getSession().getAttribute("user")!=null){
            return Result.fail(900,"退出失败");
        }
        return Result.ok("退出成功");
    }
    @GetMapping("/userExist")
    public Result userExist(@RequestParam("uname")String uName){
        User user = userService.getUserByUname(uName);
        if (user != null){
            return Result.ok();
        }
        return Result.fail(900,"用户未注册，可以使用该用户名");
    }
    @PostMapping("/reg")
    public Result register(@RequestParam("uname")String uName,
                           @RequestParam("upwd")String password,
                           @Nullable @RequestParam("nickName")String nickName,
                           @Nullable @RequestParam("motto")String motto,
                           @Nullable @RequestParam("email")String email,
                           @Nullable @RequestParam("birthday")String birthday,
                           @Nullable @RequestParam("intro")String intro,
                           @Nullable @RequestParam("area")String[] area,
                           @Nullable @RequestParam("file") MultipartFile headImg
                           ){

        return userService.register(uName,password,nickName,motto,email,birthday,intro,area,headImg);
    }
    @GetMapping("/checkUserRight")
    public Result checkUserRight(HttpServletRequest request){
        //TODO
        //获取当前登录用户
        User user = (User) request.getSession().getAttribute("user");
        return userService.checkUserRight(user);
    }
}
